#!/usr/bin/perl

use Getopt::Std;
getopts("WS:") || die "usage: $0 [-W] [-S stretch]\n";

$stretch_y = 1;
if (defined($opt_S)) {
	$stretch_y = $opt_S;
}


#$origin_y = 0.15;
#$origin_y_step = 0.113;
$origin_y = 0.25;
$origin_y_step = 0.11;

$max_y = 0.0;
$max_ty = 0.0;

$sets = 0;
$ops = 0;
while (<STDIN>) {
	chop($_);
	s/OP_//g;
	s/_/\\_/g;
	y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/;
	if (!m/^\#/) {
		if (m/^title/) {
			($tmp, $TITLE[$sets]) = split( /=/, $_ );
		} else {
			if (m/^set /) {
				$CUSTOM_CMD[$sets] = $_;
			} else {
				if (length($_) < 10) {
					$sets++;
					$ops = 0;
				} else {
					($OP[$sets][$ops], $OP_NUM[$sets][$ops], $OP_DELAY[$sets][$ops]) = split( / /, $_ );
					$OP_NUM[$sets][$ops] = $OP_NUM[$sets][$ops]/1000;
					$OP_DELAY[$sets][$ops] = $OP_DELAY[$sets][$ops]/1000000;
					if ($max_y < $OP_NUM[$sets][$ops]) {
						$max_y = $OP_NUM[$sets][$ops];
					}
					if ($max_ty < $OP_DELAY[$sets][$ops]) {
						$max_ty = $OP_DELAY[$sets][$ops];
					}
					$ops++;
				}
			}
		}
	}
}
# Handle files with no EOF 
if ($sets == 0) { $sets = 1; }

$ops=$#{$OP[0]};
$max_y = $max_y*1.1;
$max_ty = $max_ty*1.1;

$size_y = ($sets) * $origin_y_step * $stretch_y + $origin_y_step + $origin_y;
if ($opt_W) { $size_y += $origin_y_step; }

print <<"END_GNUPLOT";
#!/usr/local/bin/gnuplot

set term push;
set terminal postscript eps enhanced 'Helvetica' 14;
#set out 'DELAYS.eps'

set size 1.0,$size_y
set origin 0.0,0.0
set style fill solid 1.0

#set logscale y
set yrange [0.0:$max_y]
set xrange [-0.5:$ops]
#set noborder
set border 11
set grid x
#set grid y
set multiplot
set xlabel "bucket"

END_GNUPLOT

if ($max_y > 100) {
	print "set ytics 50,50\n";
} else {
	print "set ytics 15,15\n";
}

print "set boxwidth ", 1.0/3.0, "\n";

if ($opt_W) {
	print "set y2range [0:$max_ty]\n";
#	print "set logscale y2\n";
#	print "set y2tics border\n";
	print "set ytics nomirror\n";
	print "set y2tics nomirror\n";
	
	if ($max_ty < 500) {
		print "set y2tics 100,100\n";
	} else {
		if ($max_ty < 1500) {
			print "set y2tics 250,250\n";
		} else {
			if ($max_ty < 6000) {
				print "set y2tics 1000,1000\n";
			}
		}
	}
#	print "set y2tics 1e8,1e8\n";
	print "set grid y2\n";
#	$sets = 1;
}

for (my $s = 0; $s < $sets; $s++) {
	#print "set size 1.0,0.2\n";
	print "set size 1.0, ", $origin_y_step * $stretch_y + (0.2 - $origin_y_step), "\n";
	print "set origin 0.0,$origin_y\n";
	print "set label \'$TITLE[$s]\' at graph 0.85,0.8 right font \"Helvetica,14\"\n";
	
	if ($s == 0) {
		for (my $o = 0; $o < $ops; $o++) {
			print "set label \'$OP[0][$o]\' at graph ",(0.7+$o)/($ops+0.5),",-0.15 right rotate font \"Helvetica,12\"\n";
		}
		#print "set label \'Bucket (2^{n-1} <= x < 2^n CPU Cycles)\' at graph 0.5,-0.5 center\n";
		
		print "set label \'Operation\' at graph 0.5,-1.9 center font \"Helvetica,14\"\n";
		
		if ($opt_W) {
			print "set label \'Number of operations (x10^3)\' at graph -0.08,", $sets*0.5, " center rotate font \"Helvetica,14\"\n";
			print "set label \'Total delay (10^6 CPU cycles)\' at graph 1.09,", $sets*0.5, " center rotate font \"Helvetica,14\"\n";
		} else {
			print "set label \'Number of operations (x10^3)\' at graph -0.08,", $sets*0.5, " center rotate font \"Helvetica,14\"\n";
		}
	}
	
	if ($s == $sets - 1) {
		print "set border\n";
		print "set xtics mirror (";
	} else {
		print "set xtics nomirror (";
	}
	for (my $j = 5; $j < $max_x-5; $j += 5) {
		print "\"\" ", $j, ",";
	}
	print "\"\" ", $max_x-5, ")\n";
	
	$origin_y += $origin_y_step;
	
	print "unset xlabel\n";
	
	if ($s == $sets - 1 && $opt_W) {
		$key_y = 1.0 + 0.6 / $stretch_y;
		if (!$opt_W) {
			$key_y += ($sets - 2) * (0.2 / $stretch_y);
		}
		print "set key graph 0.97,$key_y\n";
	} else {
		print "set nokey\n";
	}
	
	print $CUSTOM_CMD[$s],"\n";
	
	if ($opt_W) {	
		print "plot '-' t 'Number of operations' with boxes fill pattern 3, '-' t 'Total delay (CPU cycles)' with boxes fill pattern 6 border 1 linetype 1 axis x1y2\n";
	} else {
		print "plot '-' t '$TITLE[0]' with boxes fill pattern 3\n";
	}
		
	for (my $o = 0; $o < $ops; $o++) {
		print $o+0.1," $OP_NUM[$s][$o]\n";
	}
	print "e\n";
	if ($opt_W) {
		for (my $o = 0; $o < $ops; $o++) {
			print $o+0.5, " $OP_DELAY[$s][$o]\n";
		}
		print "e\n";
	}
	print "unset label\n";
};

print "set nomultiplot; set out; set term pop\n";
